package com.happyfamily.springboot.crowdsourcingplatform.service.guo.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.guo.EmployerAndServiceDao;
import com.happyfamily.springboot.crowdsourcingplatform.dao.guo.ServiceDao;
import com.happyfamily.springboot.crowdsourcingplatform.dto.UserDto;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.EmployerAndServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


@Service
public class EmployerAndServiceServiceImpl implements EmployerAndServiceService
{
	@Autowired(required = false)
	private EmployerAndServiceDao employerAndServiceDao;


    //查询所有雇主
	@Override
	public Results<SysUser> getAllEmployers(Integer startPosition, Integer limit)
	{
		return Results.success(employerAndServiceDao.getSum(), employerAndServiceDao.getList(startPosition, limit));
	}

	//重置密码操作
	@Override
	public String resetPassword(Long uid)
	{
		String str = "";

		String pass=new BCryptPasswordEncoder().encode("123456");
		int i=employerAndServiceDao.resetPassword(pass,uid);
		if (i > 0)
		{
			str = "yes";
		} else
		{
			str = "no";
		}
		return str;
	}

	//更改用户状态
	@Override
	public String changeStates(Long uid, Long pid)
	{
		String str = "";
		if (pid==1){
			pid=Long.valueOf(2);
		}else if (pid==2){
			pid=Long.valueOf(1);
		}else{
			pid=Long.valueOf(3);
		}
		int i=employerAndServiceDao.changeStates(uid,pid);
		if (i > 0)
		{
			str = "yes";
		} else
		{
			str = "no";
		}
		return str;
	}

	//带条件查询雇主
	@Override
	public Results<SysUser> getSpecifyEmployers(String uname, Integer startPosition, Integer limit)
	{
		return Results.success(employerAndServiceDao.getSpecifySum(uname), employerAndServiceDao.getSpecifyList(uname,startPosition, limit));
	}

	//根据PID获取状态属性名
	@Override
	public String getEmployersStates(Long pid)
	{

		return employerAndServiceDao.getEmployersStates(pid);
	}

	//查询所有服务商
	@Override
	public Results<SysUser> getServiceProvidersList(Integer startPosition, Integer limit)
	{
		return Results.success(employerAndServiceDao.getServiceProvidersSum(2,7), employerAndServiceDao.getServiceProvidersList(2,7,startPosition, limit));
	}

	//根据PID获取状态属性名
	@Override
	public String getServiceProvidersStates(Integer id)
	{
		return employerAndServiceDao.getServiceProvidersStates(id);
	}

	//更改服务商 角色类型
	@Override
	public String changeServiceType(Integer uid,Integer ak)
	{
		String str="";
		if (ak==1){
			SysUser sysUser=employerAndServiceDao.obtainServiceOne(uid);
			if (Integer.valueOf(sysUser.getUscore())>=100){
				int n=employerAndServiceDao.changeServiceType(7,uid);

				if (n>0){
					str="yes";
				}else{
					str="fuck";
				}

			}else{
				str="no";
			}
		}else{
			int n=employerAndServiceDao.changeServiceType(2,uid);
             if (n>0){
	             str="yes";
             }else{
	             str="no";
             }
		}


		return str;
	}

	//模糊搜索服务商
	@Override
	public Results<SysUser> getSpecifiedServiceProvidersList(String uname, Integer startPosition, Integer limit)
	{
		return Results.success(employerAndServiceDao.getSpecifiedServiceProvidersSum(2,7,uname), employerAndServiceDao.getSpecifiedServiceProvidersList(2,7,uname,startPosition, limit));
	}

	//更改密码
	@Override
	public String chnagePassword(String pass, String account, String oldPass)
	{
		String str="";

		SysUser user=employerAndServiceDao.getUser(account);

		BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
		boolean matches = bc.matches(oldPass,user.getUpassword());

		if (matches){

			int i=employerAndServiceDao.changePassword(new BCryptPasswordEncoder().encode(pass),account);
			if (i>0){
				str="yes";
			}else{
				str="no";
			}

		}else{
			str="nopass";
		}
		return str;


	}

	@Override
	public Results<SysUser> getConsultantList(Integer rid1,Integer rid2,Integer startPosition, Integer limit)
	{
		return Results.success(employerAndServiceDao.getServiceProvidersSum(rid1,rid2), employerAndServiceDao.getServiceProvidersList(rid1,rid2,startPosition, limit));
	}

	@Override
	public Results<SysUser> getConsultantListByUname(Integer rid1,Integer rid2,String uname, String sid, Integer startPosition, Integer limit)
	{
		return Results.success(employerAndServiceDao.getConsultantListByUname(rid1,rid2, uname,sid).intValue(), employerAndServiceDao.getConsultantListByUnamePage(rid1, rid2,uname, sid,startPosition, limit));
	}


}
